package com.ai.cltcore.util;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;


/**
 * 字符串工具类22
 */
public final class MapUtil {
	
	private static final Logger LOGGER = LoggerFactory.getServiceLog(MapUtil.class);
	/** Private Constructor **/
	private MapUtil(){
	}
	
	public static Object mapToObject(Map<String, String> map, Class<?> beanClass) throws InstantiationException, IllegalAccessException {      
        if (map == null)    
            return null;      
        Object obj = beanClass.newInstance();    
        Field[] fields = obj.getClass().getDeclaredFields();     
        for (Field field : fields) {      
            int mod = field.getModifiers();      
            if(Modifier.isStatic(mod) || Modifier.isFinal(mod)){      
                continue;      
            }      
            field.setAccessible(true); 
            if(StringUtil.isNotEmpty(map.get(field.getName()))){
            	field.set(obj, map.get(field.getName()).trim()); 
            }
        }     
        return obj;      
    }      
     
    public static Map<String, String> objectToMap(Object obj) throws IllegalArgumentException, IllegalAccessException {      
        if(obj == null){      
            return null;      
        }     
        Map<String, String> map = new HashMap<>();      
        Field[] declaredFields = obj.getClass().getDeclaredFields();      
        for (Field field : declaredFields) {      
            field.setAccessible(true);    
            map.put(field.getName(), field.get(obj).toString());    
        }      
     
        return map;    
    }     
    public static Object map2Object(Map<String, Object> map, Class<?> beanClass) throws DedicException {      
        if (map == null)     
            return null;      
     
		try {
			Object obj = beanClass.newInstance();
			BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());      
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();      
			for (PropertyDescriptor property : propertyDescriptors) {    
				Method setter = property.getWriteMethod();      
				if (setter != null) {    
					setter.invoke(obj, map.get(property.getName()));     
				}    
			}    
			return obj;    
		} catch (Exception e) {
			LOGGER.error("", "" ,e);
            throw new DedicException(e.getMessage());
		}   
     
     
    }      
         
    public static Map<String, Object> object2Map(Object obj) throws DedicException {      
        if(obj == null)    
            return null;        
		try {
			Map<String, Object> map = new HashMap<String, Object>();     
			BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();      
			for (PropertyDescriptor property : propertyDescriptors) {      
				String key = property.getName();      
				if (key.compareToIgnoreCase("class") == 0) {     
					continue;    
				}    
				Method getter = property.getReadMethod();    
				Object value = getter!=null ? getter.invoke(obj) : null;    
				map.put(key, value);    
			}      
			return map;    
		} catch (Exception e) {
			LOGGER.error("", "", e);
			throw new DedicException(e.getMessage());
		}      
     
    }      
}
